package com.ruoyi.result.material.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.result.material.domain.dto.ResultContinuingEducationMaterialDTO;
import com.ruoyi.result.material.domain.vo.ResultContinuingEducationMaterialVO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.team.trash.domain.AchievementTrash;
import com.ruoyi.team.trash.mapper.AchievementTrashMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.result.material.mapper.ResultContinuingEducationMaterialMapper;
import com.ruoyi.result.material.domain.ResultContinuingEducationMaterial;
import com.ruoyi.result.material.service.IResultContinuingEducationMaterialService;

/**
 * 继续教育材料Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@Service
public class ResultContinuingEducationMaterialServiceImpl implements IResultContinuingEducationMaterialService
{
    @Autowired
    private ResultContinuingEducationMaterialMapper resultContinuingEducationMaterialMapper;

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AchievementTrashMapper achievementTrashMapper;
    /**
     * 查询继续教育材料
     *
     * @param id 继续教育材料主键
     * @return 继续教育材料
     */
    @Override
    public ResultContinuingEducationMaterial selectResultContinuingEducationMaterialById(Long id)
    {
        return resultContinuingEducationMaterialMapper.selectResultContinuingEducationMaterialById(id);
    }
    /**
     * 批量获取其他上传
     */
    @Override
    public List<ResultContinuingEducationMaterialVO> selectResultContinuingEducationMaterialByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Collections.emptyList();
        }
        List<ResultContinuingEducationMaterial> resultContinuingEducationMaterialList = resultContinuingEducationMaterialMapper.selectResultContinuingEducationMaterialByIds(ids);
        List<ResultContinuingEducationMaterialVO> resultContinuingEducationMaterialVOList = new ArrayList<>();

        for (ResultContinuingEducationMaterial resultPaper : resultContinuingEducationMaterialList) {
            ResultContinuingEducationMaterialVO resultContinuingEducationMaterialVO = new ResultContinuingEducationMaterialVO();
            // 假设 resultContinuingEducationMaterial 和 resultContinuingEducationMaterialVO 有相同的字段，先进行复制
            BeanUtils.copyProperties(resultPaper, resultContinuingEducationMaterialVO);

            // 根据 userId 查询 nick_name
            SysUser sysUser = sysUserMapper.selectUserById(resultPaper.getUserId());
            resultContinuingEducationMaterialVO.setNickName(sysUser.getNickName());

            resultContinuingEducationMaterialVOList.add(resultContinuingEducationMaterialVO);
        }
        return resultContinuingEducationMaterialVOList;
    }
    /**
     * 查询继续教育材料列表
     *
     * @param resultContinuingEducationMaterial 继续教育材料
     * @return 继续教育材料
     */
    /**
     * 查询继续教育材料列表
     *
     * @param resultContinuingEducationMaterialDTO 查询DTO
     * @return TableDataInfo 表格数据
     */
    @Override
    public TableDataInfo selectResultContinuingEducationMaterialList(ResultContinuingEducationMaterialDTO resultContinuingEducationMaterialDTO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long deptId = loginUser.getUser().getDeptId();

        // 判断是否为超级管理员（若依默认超级管理员用户ID为1）
        boolean isSuperAdmin = loginUser.getUser().getUserId().equals(1L);

        // 非超级管理员才进行部门过滤
        if (!isSuperAdmin) {
            resultContinuingEducationMaterialDTO.setDeptId(deptId);
        }

        // 分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (StringUtils.isNotNull(pageDomain.getPageNum()) && StringUtils.isNotNull(pageDomain.getPageSize())) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize(), orderBy)
                    .setReasonable(pageDomain.getReasonable());
        }

        // 查询继续教育材料列表
        List<ResultContinuingEducationMaterial> list = resultContinuingEducationMaterialMapper.selectResultContinuingEducationMaterialListByDept(resultContinuingEducationMaterialDTO);

        // 转换为VO并填充用户昵称
        List<ResultContinuingEducationMaterialVO> voList = list.stream().map(r -> {
            ResultContinuingEducationMaterialVO vo = new ResultContinuingEducationMaterialVO();
            BeanUtils.copyProperties(r, vo);
            if (r.getUserId() != null) {
                SysUser sysUser = sysUserMapper.selectUserById(r.getUserId());
                if (sysUser != null) {
                    vo.setNickName(sysUser.getNickName());
                }
            }
            return vo;
        }).collect(Collectors.toList());

        // 返回表格数据
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        rspData.setRows(voList);
        return rspData;
    }

    /**
     * 新增继续教育材料
     *
     * @param resultContinuingEducationMaterial 继续教育材料
     * @return 结果
     */
    @Override
    public int insertResultContinuingEducationMaterial(ResultContinuingEducationMaterial resultContinuingEducationMaterial)
    {
        return resultContinuingEducationMaterialMapper.insertResultContinuingEducationMaterial(resultContinuingEducationMaterial);
    }

    /**
     * 修改继续教育材料
     *
     * @param resultContinuingEducationMaterial 继续教育材料
     * @return 结果
     */
    @Override
    public int updateResultContinuingEducationMaterial(ResultContinuingEducationMaterial resultContinuingEducationMaterial)
    {
        return resultContinuingEducationMaterialMapper.updateResultContinuingEducationMaterial(resultContinuingEducationMaterial);
    }

    /**
     * 批量删除继续教育材料
     *
     * @param ids 需要删除的继续教育材料主键
     * @return 结果
     */
    @Override
    public int deleteResultContinuingEducationMaterialByIds(Long[] ids)
    {
        List<ResultContinuingEducationMaterial> resultContinuingEducationMaterialList = resultContinuingEducationMaterialMapper.selectResultContinuingEducationMaterialByIds(ids);
        for (ResultContinuingEducationMaterial resultContinuingEducationMaterial : resultContinuingEducationMaterialList) {
            AchievementTrash achievementTrash = new AchievementTrash();
            achievementTrash.setUserid(resultContinuingEducationMaterial.getUserId());
            achievementTrash.setAchievementType("继续教育材料");
            achievementTrash.setTitle(resultContinuingEducationMaterial.getTitle());
            achievementTrash.setAchievementId(resultContinuingEducationMaterial.getId());
            achievementTrash.setDeletedAt(new Date());
            achievementTrashMapper.insertAchievementTrash(achievementTrash);
        }
        return resultContinuingEducationMaterialMapper.deleteResultContinuingEducationMaterialByIds(ids);
    }

    /**
     * 删除继续教育材料信息
     *
     * @param id 继续教育材料主键
     * @return 结果
     */
    @Override
    public int deleteResultContinuingEducationMaterialById(Long id)
    {
        return resultContinuingEducationMaterialMapper.deleteResultContinuingEducationMaterialById(id);
    }
}
